<!DOCTYPE html>
<html>
<head>
    <title>添加医生--中软高科-2015</title>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" href="../../Css/bootstrap.css"/>
    <link rel="stylesheet" type="text/css" href="../../Css/bootstrap-responsive.css"/>
    <link rel="stylesheet" type="text/css" href="../../Css/style.css"/>
    <script type="text/javascript" src="../../Js/jquery.js"></script>
    <script type="text/javascript" src="../Js/jquery.sorted.js"></script>
    <script type="text/javascript" src="../../Js/bootstrap.js"></script>
    <script type="text/javascript" src="../../Js/ckform.js"></script>
    <script type="text/javascript" src="../../Js/common.js"></script>
    <script type="text/javascript" src="../../Js/ckeditor/ckeditor.js"></script>
    <!-- 引入身份证验证 js 库 -->
    <script type="text/javascript" src="../../Js/cusz/IdCard-Validate.js"></script>


    <style type="text/css">
        body {
            padding-bottom: 40px;
        }

        .sidebar-nav {
            padding: 9px 0;
        }

        @media (max-width: 980px) {
            /* Enable use of floated navbar text */
            .navbar-text.pull-right {
                float: none;
                padding-left: 5px;
                padding-right: 5px;
            }
        }


    </style>
    <script type="text/javascript">
        $(function () {
            $('#backid').click(function () {
                window.location.href = "index.html";
            });
        });
    </script>
</head>
<body>
<form action="index.html" id="doctorForm" method="post" class="definewidth m20">
    <table class="table table-bordered table-hover definewidth m10">
        <tr>
            <td width="10%" class="tableleft">姓名</td>
            <td><input type="text" name="dName" value=""/></td>
        </tr>

        <tr>
            <td width="10%" class="tableleft">身份证号</td>
            <td><input onblur="idCarCheck()" type="text" name="dIdcar" value="512501197506045175"/></td>
        </tr>
        <tr>
            <td width="10%" class="tableleft">手机</td>
            <td><input type="text" name="dPhone" value=""/></td>
        </tr>
        <tr>
            <td width="10%" class="tableleft">座机</td>
            <td><input type="text" name="dTelphone" value=""/></td>
        </tr>
        <tr>
            <td width="10%" class="tableleft">性别</td>
            <td><input disabled type="radio" name="dSex" value="0" checked/>男&nbsp;&nbsp;&nbsp;<input disabled
                                                                                                      type="radio"
                                                                                                      name="dSex"
                                                                                                      value="1"/>女
            </td>
        </tr>
        <tr>
            <td width="10%" class="tableleft">出生年月</td>
            <td><input disabled type="text" name="dBirthday"/></td>
        </tr>
        <tr>
            <td width="10%" class="tableleft">年龄</td>
            <td id="dAge">28</td>
        </tr>
        <tr>
            <td width="10%" class="tableleft">电子邮箱</td>
            <td><input type="text" name="dEmail" value=""/></td>
        </tr>
        <tr>
            <td width="10%" class="tableleft">所属科室</td>
            <td>
                <select name="dKeshi">
                    <option value="">---</option>
                    <option value="急诊科">急诊科</option>
                    <option value="骨科">骨科</option>
                    <option value="血液科">血液科</option>
                </select>
            </td>
        </tr>
        <tr>
            <td width="10%" class="tableleft">学历</td>
            <td>
                <select name="dXueli">
                    <option value="">---</option>
                    <option value="专科">专科</option>
                    <option value="本科">本科</option>
                    <option value="博士">博士</option>
                    <option value="博士后">博士后</option>
                </select>
            </td>
        </tr>
        <tr>
            <td width="10%" class="tableleft">备注</td>
            <td><textarea id="dDesc"></textarea></td>
        </tr>
        <tr>
            <td colspan="2">
                <center>
                    <button onclick="addDoctor()" class="btn btn-primary" type="button">保存</button> &nbsp;&nbsp;<button
                        type="button" class="btn btn-success" name="backid" id="backid">返回列表
                </button>
                </center>
            </td>
        </tr>
    </table>
</form>

<script>
    $(function () {
        idCarCheck();
    })

    function addDoctor() {
        // 不使用表单方式提交请求
        event.preventDefault();
        // let doctor = $("#doctorForm").serialize();
        let dName = $("input[name='dName']").val();
        let dIdcar = $("input[name='dIdcar']").val();
        let dPhone = $("input[name='dPhone']").val();
        let dTelphone = $("input[name='dTelphone']").val();
        let dSex = $("input[name='dSex']:checked").val();
        let dBirthday = $("input[name='dBirthday']").val();
        let dAge = $("#dAge").html();
        let dEmail = $("input[name='dEmail']").val();
        let dKeshi = $("select[name='dKeshi']").val();
        let dXueli = $("select[name='dXueli']").val();
        let dDesc = document.getElementById('dDesc').value;

        let doctor = {
            dName, dIdcar, dPhone, dTelphone, dSex, dBirthday, dAge, dEmail, dKeshi, dXueli, dDesc
        }

        console.log(doctor)

        $.ajax({
            type: "POST",
            url: "/doctor/save",
            dataType: "json",
            data: {
                ...doctor
            },
            success: function (res) {
                alert(res.msg);
                if (res.code === 200) {
                    // 跳转至查询页
                    $(location).attr("href", "index.html");
                }
            }
        });
    }

    /**
     * 身份证验证
     */
    function idCarCheck() {
        let dIdcar = $("input[name='dIdcar']").val();
        console.log(dIdcar);
        if (dIdcar == undefined || dIdcar == '' || dIdcar.length == 0) {
            return;
        }
        let f = validateIdCard(dIdcar);
        // console.log(dIdcar);
        if (!f) {
            alert("请输入正确的身份证号!")
            return;
        }
        // 根据身份证号码获取生日/性别等信息
        let sex = IdCard(dIdcar, 2);
        let sexIn = $(`input[name='dSex'][value='${sex}']`);
        let sexOut = $(`input[name='dSex'][value!='${sex}']`);
        sexOut.prop("checked", false);
        sexIn.prop("checked", "checked");

        let birth = IdCard(dIdcar, 1);
        $("input[name='dBirthday']").val(birth);

        let age = IdCard(dIdcar, 3);
        $("#dAge").html(age);
    }

    // 根据身份证号获取出生日期/性别/年龄
    function IdCard(UUserCard, num) {
        if (num == 1) {
            //获取出生日期
            birth = UUserCard.substring(6, 10) + "-" + UUserCard.substring(10, 12) + "-" + UUserCard.substring(12, 14);
            return birth;
        }
        if (num == 2) {
            //获取性别
            if (parseInt(UUserCard.substr(16, 1)) % 2 == 1) {
                //男
                return 0;
            } else {
                //女
                return 1;
            }
        }
        if (num == 3) {
            //获取年龄
            var myDate = new Date();
            var month = myDate.getMonth() + 1;
            var day = myDate.getDate();
            var age = myDate.getFullYear() - UUserCard.substring(6, 10) - 1;
            if (UUserCard.substring(10, 12) < month || UUserCard.substring(10, 12) == month && UUserCard.substring(12, 14) <= day) {
                age++;
            }
            return age;
        }
    }
</script>

</body>
</html>